package com.example.readroomforpaging3

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.viewModels
import com.example.readroomforpaging3.databinding.FragmentMainBinding
import com.example.readroomforpaging3.viewmodel.MainViewModel


class MainFragment : Fragment() {

    companion object {
        fun newInstance() = MainFragment()
    }

    private lateinit var binding: FragmentMainBinding

    private val viewModel: MainViewModel by viewModels()


    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        binding = FragmentMainBinding.inflate(inflater)
        return binding.root
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        val word = "who are you?"
        binding.textview.text = "正在翻译……"
        viewModel.translateResult.observe(viewLifecycleOwner) {
            binding.textview.text = "原词:    $word \n 翻译:    $it"
        }

        viewModel.translate(word)
    }

}